﻿using System;
using System.Text;
using System.Web;
using Dexter.Dependency;
using Dexter.PluginManager;
using Dexter.Services;
using Dexter.Web.Mvc.ViewModel.ViewsData;
using Dexter.Web.Mvc.ViewsData;

namespace Dexter.Plugins.IEPinning {
	public class PinningPlugin : IPlugin {
		readonly IUrlBuilder urlBuilder;

		public PinningPlugin ( IUrlBuilder urlBuilder ) {
			this.urlBuilder = urlBuilder;
		}

		public void ChangeModel ( HttpContextBase context , PublicBaseViewData baseViewData ) {
			StringBuilder sb = new StringBuilder ( );

			//var pinningSiteConfiguration = pluginManager.GetConfiguration<PinningSiteConfiguration> ( this );

			sb.AppendFormat ( "<meta name=\"msapplication-starturl\" content=\"{0}\"/>", urlBuilder.HomePage );
			sb.AppendLine ( );
			sb.AppendLine ( "<meta name=\"msapplication-window\" content=\"width=1024;height=768\"/>" );
			sb.AppendLine ( "<meta name=\"msapplication-navbutton-color\" content=\"#3399FF\"/>" );
			sb.AppendFormat ( "<meta name=\"msapplication-task\" content=\"name=Blog;action-uri={0};icon-uri={1}\" />", urlBuilder.HomePage, urlBuilder.ResolveUrl ( "~/Images/bg-blog.ico" ) );
			sb.AppendLine ( );
			sb.AppendFormat ( "<meta name=\"msapplication-task\" content=\"name=Contact;action-uri={0};icon-uri={1}\" />", urlBuilder.ContactHome, urlBuilder.ResolveUrl ( "~/Images/bg-comments.ico" ) );
			sb.AppendLine ( );
			sb.AppendFormat ( "<meta name=\"msapplication-task\" content=\"name=Search;action-uri={0};icon-uri={1}\" />", urlBuilder.SearchHome, urlBuilder.ResolveUrl ( "~/Images/bg-submit.ico" ) );
			sb.AppendLine ( );

			//if (!string.IsNullOrEmpty ( pinningSiteConfiguration.TwitterName )) {
			//    sb.AppendFormat ( "<meta name=\"msapplication-task\" content=\"name=Twitter;action-uri=http://twitter.com/{0}/;icon-uri={1}\" />", pinningSiteConfiguration.TwitterName, urlBuilder.ResolveUrl ( "~/Images/bg-twitter.ico" ) );
			//}

			sb.AppendLine ( "<script type=\"text/javascript\">" );
			sb.AppendLine ( "if (window.external.msIsSiteMode()) {" );
			sb.AppendLine ( "window.external.msSiteModeClearJumpList();" );
			sb.AppendLine ( "window.external.msSiteModeCreateJumpList('Build My Pinned Site');" );

			foreach (var post in baseViewData.RecentPost) {
				sb.AppendFormat ( "window.external.msSiteModeAddJumpListItem('{0}', '{1}', \'{2}\', 'self');", post.Title, urlBuilder.Post.Detail ( post ), urlBuilder.ResolveUrl ( "~/Images/bg-comments.ico" ) );
				sb.AppendLine ( );
			}

			sb.Append ( "}" );
			sb.AppendLine ( );
			sb.Append ( "</script>" );

			baseViewData.Configuration.HeaderCode += sb.ToString ( );
		}

		public void Setup() {
		}

		public object GetConfiguration() {
			return null;
		}

		public void RegisterServices(IDexterContainer container) {

		}
	}
}